package com.app.aipass.activity;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.ActivityForgetPwdFirstBinding;
import com.app.aipass.databinding.ActivityLoginBinding;
import com.app.aipass.entity.FindPswEntity;
import com.app.aipass.utils.ToastUtils;
import com.app.aipass.viewmodel.LoginViewModel;

import me.goldze.mvvmhabit.base.BaseActivity;

public class ForgetPwdFirstActivity extends BaseActivity<ActivityForgetPwdFirstBinding, LoginViewModel> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_forget_pwd_first;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(LoginViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = binding.etAccount.getText().toString();
                if (!phone.isEmpty()){
                    viewModel.smsCode(phone,binding.tvSend);
                }else {
                    ToastUtils.toastLong("请输入手机号");
                }
            }
        });

        binding.tvBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = binding.etAccount.getText().toString();
                String code = binding.etCode.getText().toString();
                String pwd = binding.etPwd.getText().toString();
                String again = binding.etAgain.getText().toString();
                if (phone.isEmpty()) {
                    ToastUtils.toastShort("请输入手机号");
                    return;
                }
                if (code.isEmpty()) {
                    ToastUtils.toastShort("请输入验证码");
                    return;
                }
                if (!pwd.equals(again)) {
                    ToastUtils.toastShort("两次密码不一致");
                    return;
                }
                viewModel.findPassword(phone,code,pwd);
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.mFindPswEntity.observe(this, new Observer<FindPswEntity>() {
            @Override
            public void onChanged(FindPswEntity findPswEntity) {
                ToastUtils.toastLong("修改成功");
                finish();
            }
        });
    }
}
